#include "MainMenuScene.hpp"


MainMenuScene::~MainMenuScene()
{
    for (Component *com : components)
    {
        delete com;
    }
    components.clear();
}

void MainMenuScene::init()
{

    btn_play = new Button("开始游戏", {100, 100});
    btn_continue = new Button("继续游戏", {100, 200});
    btn_option = new Button("游戏设置", {100, 300});
    btn_exit = new Button("退出游戏", {100, 400});

    SDL_Rect rc = btn_play->getRect();
    int center_x_begin = (this->width - rc.w) / 2;
    btn_play->setPosition(center_x_begin, 100);
    btn_continue->setPosition(center_x_begin, 200);
    btn_option->setPosition(center_x_begin, 300);
    btn_exit->setPosition(center_x_begin, 400);
    this->components.push_back(btn_play);
    this->components.push_back(btn_continue);
    this->components.push_back(btn_option);
    this->components.push_back(btn_exit);
}

int MainMenuScene::handleEvent(SDL_Event &evt)
{
    int res = 0;
    for (Component *com : components)
    {
        res = com->handleEvent(evt);
        if (res)
        {
            if (evt.type == SDL_MOUSEBUTTONUP)
            {
                if (com == btn_exit)
                {
                    return -2;
                }
            }
        }
    }
    return 0;
}

void MainMenuScene::render(SDL_Renderer *renderer)
{
    for (Component *com : components)
    {
        com->render(renderer);
    }
}
